package com.faithbj.controller.statistics;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.faithbj.common.model.Region;
import com.faithbj.common.model.User;
import com.faithbj.common.utils.DateUtils;
import com.faithbj.constant.Constant;
import com.faithbj.model.conditions.DiffFamilyStatResult;
import com.faithbj.model.conditions.ReasonResult;
import com.faithbj.model.conditions.StreetDiffFamilyData;
import com.faithbj.process.State;
import com.faithbj.process.stats.JbpmStatsService;
import com.faithbj.process.stats.PauseReason;
import com.faithbj.process.stats.TerminateReason;
import com.faithbj.service.statistics.TestJbpmStatsService;

/**
 * 差异情况统计
 * @author m
 *
 */
@Controller
@RequestMapping("/diffInfoStat")
public class DiffInfoStatController extends StatBaseController {

//	@Resource因jbpm还未实现，暂时注释掉
	private JbpmStatsService jbpmStatService;
	@Resource
	private TestJbpmStatsService testJbpmStatsService;
	
	/**
	 * 差异家庭户数统计
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/diffFamilyStat")
	public String diffFamilyStat(HttpServletRequest request, ModelMap model) {
		//初始化区县列表
		this.initCountryRegionList();
		
		String acceptStartTime = request.getParameter("acceptStartTime");
		String acceptEndTime = request.getParameter("acceptEndTime");
		String countiesRegionId = request.getParameter("countiesRegionId");
		String streetRegionId = request.getParameter("streetRegionId");
		Date from = null;
		Date to = null;
		if("1".equals(request.getParameter("type"))){
			to = new Date();
			from = this.getStartTime(to);
		} else {
			if(StringUtils.isNotBlank(acceptStartTime)){
				from = DateUtils.getStringToDate(acceptStartTime, DateUtils.DateFormatDefault);
			}
			if(StringUtils.isNotBlank(acceptEndTime)) {
				to = DateUtils.getStringToDate(acceptEndTime, DateUtils.DateFormatDefault);
			}
		}
		
		//从jbpm的接口中获取数据
		String regionId = "";
		if(!StringUtils.isBlank(streetRegionId)) {
			regionId = streetRegionId;
		} else if(!StringUtils.isBlank(countiesRegionId)) {
			regionId = countiesRegionId;
		} else {
			User user = this.getLoginUser();
			regionId = "402877813833cb0c013833db09650005";
			if(user != null && user.getRegion() != null) {
				regionId = user.getRegion().getId();
			} 			
		}
		Map<String, Map<State, Long>> resultMap = new HashMap<String, Map<State, Long>>();
		if(jbpmStatService != null){
			resultMap = jbpmStatService.countGroupByApplyStateInRegion(regionId, from, to);
		} else {
			resultMap = testJbpmStatsService.countGroupByApplyStateInRegion(regionId, from, to);
		}			

		List<Region> countryRegionList = this.getCountryRegionList(countiesRegionId, streetRegionId);		
		
		List<DiffFamilyStatResult> result = new ArrayList<DiffFamilyStatResult>();
		if(countryRegionList != null){
			for(Region region : countryRegionList) {
				if(region.getChildren() == null || region.getChildren().size() == 0){
					continue;
				}
				DiffFamilyStatResult diffFamilyStatResult = new DiffFamilyStatResult();
				diffFamilyStatResult.setCountryRegionName(region.getName());
				List<StreetDiffFamilyData> streetDiffFamilyDataList = new ArrayList<StreetDiffFamilyData>();
				for(Region streetRegion : region.getChildren()) {
					StreetDiffFamilyData data = new StreetDiffFamilyData();
					data.setStreetRegionName(streetRegion.getName());
					Map<State, Long> stateMap = resultMap.get(streetRegion.getId());
					Iterator it = stateMap.keySet().iterator();
					while(it.hasNext()) {
						State key = (State)it.next();
						Long value = stateMap.get(key);
						if(key.getId().equals(Constant.PAUSE_CHECK_STATE)) {
							data.setSuspendCheckNum(value);
						} else if(key.getId().equals(Constant.DIFF_STATE)) {
							data.setDiffNum(value);
						} else if(key.getId().equals(Constant.CHECK_REPORT_STATE)) {
							data.setCheckReportNum(value);
						} else if(key.getId().equals(Constant.TERMINAL_CHECK_STATE)) {
							data.setTerminateCheckNum(value);
						}
					}
					streetDiffFamilyDataList.add(data);
				}
				diffFamilyStatResult.setStreetDiffFamilyDataList(streetDiffFamilyDataList);
				result.add(diffFamilyStatResult);
			}
		}	
		
		model.put("resultList", result);
		model.put("countryRegionList", this.countryRegionList);
		return "/analysis/diffHomeAmountStatistics";
	}
	
	/**
	 * 中止情况原因统计
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/suspendReasonStat")
	public String suspendReasonStat(HttpServletRequest request, ModelMap model) {
		//初始化区县列表
		this.initCountryRegionList();
		
		String acceptStartTime = request.getParameter("acceptStartTime");
		String acceptEndTime = request.getParameter("acceptEndTime");
		String countiesRegionId = request.getParameter("countiesRegionId");
		String streetRegionId = request.getParameter("streetRegionId");
		Date from = null;
		Date to = null;
		if("1".equals(request.getParameter("type"))){
			to = new Date();
			from = this.getStartTime(to);
		} else {
			if(StringUtils.isNotBlank(acceptStartTime)){
				from = DateUtils.getStringToDate(acceptStartTime, DateUtils.DateFormatDefault);
			}
			if(StringUtils.isNotBlank(acceptEndTime)) {
				to = DateUtils.getStringToDate(acceptEndTime, DateUtils.DateFormatDefault);
			}
		}
		
		//从jbpm的接口中获取数据：请求参数：中止核对操作日期区间，regionId区域Id; 返回：中止原因列表（包括中止原因及中止家庭户数）
		String regionId = "";
		if(!StringUtils.isBlank(streetRegionId)) {
			regionId = streetRegionId;
		} else if(!StringUtils.isBlank(countiesRegionId)) {
			regionId = countiesRegionId;
		} else {
			User user = this.getLoginUser();
			regionId = "402877813833cb0c013833db09650005";
			if(user != null && user.getRegion() != null) {
				regionId = user.getRegion().getId();
			} 			
		}
		Map<PauseReason, Long> resultMap = new HashMap<PauseReason, Long>();
		if(jbpmStatService != null){
			resultMap = jbpmStatService.getPauseReasonDetailsInRegion(regionId, from, to);
		} else {
			resultMap = testJbpmStatsService.getPauseReasonDetailsInRegion(regionId, from, to);
		}
			
		List<ReasonResult> result = new ArrayList<ReasonResult>();
		Iterator it = resultMap.keySet().iterator();
		int i = 1;
		while(it.hasNext()) {
			PauseReason key = (PauseReason)it.next();
			Long value = resultMap.get(key);
			ReasonResult reason = new ReasonResult();
			reason.setId(i + "");
			reason.setReason(key.getName());
			reason.setNum(value + "");
			result.add(reason);
			i++;
		}
		
		model.put("resultList", result);
		model.put("countryRegionList", this.countryRegionList);
		return "/analysis/suspendReasonStatistics";
	}
	
	/**
	 * 终止情况原因统计
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/terminalReasonStat")
	public String terminalReasonStat(HttpServletRequest request, ModelMap model) {
		//初始化区县列表
		this.initCountryRegionList();
		
		String acceptStartTime = request.getParameter("acceptStartTime");
		String acceptEndTime = request.getParameter("acceptEndTime");
		String countiesRegionId = request.getParameter("countiesRegionId");
		String streetRegionId = request.getParameter("streetRegionId");
		Date from = null;
		Date to = null;
		if("1".equals(request.getParameter("type"))){
			to = new Date();
			from = this.getStartTime(to);
		} else {
			if(StringUtils.isNotBlank(acceptStartTime)){
				from = DateUtils.getStringToDate(acceptStartTime, DateUtils.DateFormatDefault);
			}
			if(StringUtils.isNotBlank(acceptEndTime)) {
				to = DateUtils.getStringToDate(acceptEndTime, DateUtils.DateFormatDefault);
			}
		}
		
		//从jbpm的接口中获取数据：请求参数：终止核对操作日期区间，regionId区域Id; 返回：终止原因列表（包括终止原因及终止家庭户数）
		String regionId = "";
		if(!StringUtils.isBlank(streetRegionId)) {
			regionId = streetRegionId;
		} else if(!StringUtils.isBlank(countiesRegionId)) {
			regionId = countiesRegionId;
		} else {
			User user = this.getLoginUser();
			regionId = "402877813833cb0c013833db09650005";
			if(user != null && user.getRegion() != null) {
				regionId = user.getRegion().getId();
			} 				
		}
		
		Map<TerminateReason, Long> resultMap = new HashMap<TerminateReason, Long>();
		if(jbpmStatService != null){
			resultMap = jbpmStatService.getTerminateReasonDetailsInRegion(regionId, from, to);
		} else {
			resultMap = testJbpmStatsService.getTerminateReasonDetailsInRegion(regionId, from, to);
		}
					 
		List<ReasonResult> result = new ArrayList<ReasonResult>();
		
		Iterator it = resultMap.keySet().iterator();
		int i = 1;
		while(it.hasNext()) {
			TerminateReason key = (TerminateReason)it.next();
			Long value = resultMap.get(key);
			ReasonResult reason = new ReasonResult();
			reason.setId(i + "");
			reason.setReason(key.getName());
			reason.setNum(value + "");
			result.add(reason);
			i++;
		}
		
		model.put("resultList", result);
		model.put("countryRegionList", this.countryRegionList);
		return "/analysis/stopReasonStatistics";
	}
	
}
